//
//  NewFeautreViewController.swift
//  Weibo
//
//  Created by gdarkness on 16/3/26.
//  Copyright © 2016年 gdarkness. All rights reserved.
//

import UIKit

private let reuseIdentifier = "Cell"

class NewFeautreViewController: UICollectionViewController {
    
    //流水布局
    let layout  = UICollectionViewFlowLayout()
    
    //父类没有init()这个构造函数，子类不需要使用override
    init() {
        super.init(collectionViewLayout: layout)
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        prepareCollectionView()
    }
    
    //collectionView 设置
    private func prepareCollectionView() {
        //设置layout参数
        layout.itemSize = UIScreen.mainScreen().bounds.size
        layout.minimumLineSpacing = 0
        layout.minimumInteritemSpacing = 0
        layout.scrollDirection = UICollectionViewScrollDirection.Horizontal
        
        collectionView?.pagingEnabled = true
        collectionView?.bounces = false
        
        //注册cell
       collectionView?.registerClass(NewFeatureCell.self, forCellWithReuseIdentifier: reuseIdentifier)
    }
}

extension NewFeautreViewController {
    override func  collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return NewFeatuerPageCount
    }
    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        //从缓存池里面获取cell
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! NewFeatureCell
        
        cell.backgroundColor = UIColor.random()
        
        //设置cell要显示的页码
        cell.index = indexPath.item
        
        return cell
        
    }
    override func scrollViewDidEndDecelerating(scrollView: UIScrollView) {
        //计算当前页面
        let page =  Int(scrollView.contentOffset.x / UIScreen.mainScreen().bounds.width)
        print("page: \(page)")
        //判断当前是否最后一页
        if page == NewFeatuerPageCount - 1 {
            //创建indexPath
            let indexPath = NSIndexPath(forItem: page, inSection: 0)
            
            //获取indePath对应的cell
            let cell  = collectionView?.cellForItemAtIndexPath(indexPath) as! NewFeatureCell
            
            //让开始按钮动画
            cell.startButtonAnimation()
        }
    }
}
class NewFeatureCell: UICollectionViewCell {
    
    //MARK: --属性
    //显示当前页数
    var index: Int = 0 {
        didSet{
            //在控制器设置了页码之后，根据当前页面来显示对应的图片
            //图片1，2，3，4
            //index0,1,2,3
            backgroundImageView.image = UIImage(named: "new_feature_\(index + 1)")
            
            //只要cell要设置背景图片，就隐蔽按钮
            startButton.hidden = true
        }
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        prepareUI()
    }
    
    func didClickStartButton() {
        //切换懂啊mainViewController
        AppDelegate.outSwitchRootViewController(MainViewController())
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
    }

    //开始动画
    func startButtonAnimation() {
        startButton.hidden = false
        
        //在开始动画之间，将transform scale 设置为0
        startButton.transform = CGAffineTransformMakeScale(0, 0)
        
        UIView.animateWithDuration(1, delay: 0.1, usingSpringWithDamping: 0.5, initialSpringVelocity: 6, options: UIViewAnimationOptions(rawValue: 0), animations: { () -> Void in
            self.startButton.transform = CGAffineTransformIdentity
            }, completion: nil)
    }
    
    
    
    private func prepareUI() {
        //添加子控件，注意王contenView里面添加
        contentView.addSubview(backgroundImageView)
        contentView.addSubview(startButton)
        
        //添加约束
        backgroundImageView.translatesAutoresizingMaskIntoConstraints = false
        startButton.translatesAutoresizingMaskIntoConstraints = false
        
        //背景
        backgroundImageView.snp_makeConstraints { (make) -> Void in
            make.edges.equalTo(contentView)
        }
//        contentView.addConstraint( NSLayoutConstraint(item: backgroundImageView, attribute: NSLayoutAttribute.Leading, relatedBy: NSLayoutRelation.Equal, toItem: contentView, attribute: NSLayoutAttribute.Leading, multiplier: 1, constant: 0))
//        
//        contentView.addConstraint( NSLayoutConstraint(item: backgroundImageView, attribute: NSLayoutAttribute.Top   , relatedBy: NSLayoutRelation.Equal, toItem: contentView, attribute: NSLayoutAttribute.Top, multiplier: 1, constant: 0))
//        
//        contentView.addConstraint( NSLayoutConstraint(item: backgroundImageView, attribute: NSLayoutAttribute.Trailing, relatedBy: NSLayoutRelation.Equal, toItem: contentView, attribute: NSLayoutAttribute.Trailing, multiplier: 1, constant: 0))
//        
//        contentView.addConstraint( NSLayoutConstraint(item: backgroundImageView, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: contentView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0))
        
        //开始按钮
        startButton.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(contentView)
            make.bottom.equalTo(contentView).offset(-160)
        }
//        contentView.addConstraint( NSLayoutConstraint(item: startButton, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: contentView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
//
//        contentView.addConstraint( NSLayoutConstraint(item: startButton, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: contentView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: -160))
       
    }
    
    //MARK: -- lazyload
    private lazy var backgroundImageView: UIImageView = UIImageView(image: UIImage(named: "new_feature_1"))
    
    private lazy var startButton: UIButton = {
        let button = UIButton()
        
        button.setTitle("开始体验", forState: UIControlState.Normal)
        button.setTitleColor(UIColor.whiteColor(), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "new_feature_finish_button"), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "new_feature_finish_button_highlighted"), forState: UIControlState.Highlighted)
        button.addTarget(self, action: "didClickStartButton", forControlEvents: UIControlEvents.TouchUpInside)
        return button
    }()
    
}



